package com.yeyks.appcontroller;

import com.alibaba.fastjson.JSONObject;
import com.yeyks.base.Arguments;
import com.yeyks.constant.ConfigKey;
import com.yeyks.dal.domain.ShareLog;
import com.yeyks.dal.domain.UserInfo;
import com.yeyks.exception.ServiceException;
import com.yeyks.model.Ajax.AjaxResponseBody;
import com.yeyks.service.ConfigService;
import com.yeyks.service.ShareLogService;
import com.yeyks.service.UserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;

/**
 * @Author fuchangshuai
 * @date 2019/3/26 5:10 PM
 */
@Api(tags = "分享接口", description = "分享")
@Slf4j
@RestController("appShareLogController")
@RequestMapping(value = "/app/share")
public class ShareLogController {
    @Autowired
    private ConfigService configService;
    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private ShareLogService shareLogService;

    /**
     * 获取分享文案
     *
     * @return
     */
    @ApiOperation(value = "获取分享文案", notes = "获取分享文案")
    @RequestMapping(value = "/content", method = RequestMethod.POST)
    public ResponseEntity<?> shareContent() {
        String value = configService.getValue(ConfigKey.SHARE_CONTENT);
        if (!Arguments.notEmpty(value)) {
            return ResponseEntity.ok(AjaxResponseBody.success(new HashMap<>()));
        }
        return ResponseEntity.ok(AjaxResponseBody.success(JSONObject.parseObject(value)));
    }

    /**
     * 分享
     *
     * @param token
     * @param type
     * @return
     */
    @ApiOperation(value = "用户分享", notes = "用户分享")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "token", value = "token", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "type", value = "分享类型(微博WEIBO，朋友圈TIMELINE，微信APP_MESSAGE)", required = true, dataType = "String")
    })
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public ResponseEntity<?> add(
            @RequestParam(name = "token") String token,
            @RequestParam(name = "type") String type) {

        UserInfo accountInfo = userInfoService.tokenUserInfo(token);
        ShareLog shareLog = new ShareLog();
        shareLog.setUserId(accountInfo.getId());
        shareLog.setShareType(type.toUpperCase());
        shareLog.setGmtCreate(new Date());
        int insert = shareLogService.insert(shareLog);
        if (insert != 1) {
            log.info("分享更新数据失败");
            throw new ServiceException("更新数据失败");
        }
        return ResponseEntity.ok(AjaxResponseBody.success());
    }
}
